<?php

namespace app\api\controller\pifa;

use app\common\controller\Api;
use think\Config;
use think\Db;
use think\Validate;
use think\Session;
use fast\WechatMini;
use addons\third\model\Third;
use think\Log;
use think\Request;
use fast\Http;
use think\Exception;
use addons\third\library\Service;
use app\api\model\Peisong;
/**
 * 首页接口
 */
class Kuguan extends Api
{
    protected $noNeedLogin = ['login','getconfig','getMendian1','token','laobaninit'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        $this->token = $this->request->post('token');

        parent::_initialize();

    } 

    public function peisonglist()
    {  
        $list = Db::table('fa_pifa_peisongyuan')->order('createtime desc')->paginate(12);
        $this->success("成功", $list);
    }


    public function morenpeisong()
    {
        $post = $this->request->request();
        $updata['is_default'] = 0;
        $map['id'] = ['<>',0];
        Db::table('fa_pifa_peisongyuan')->where($map)->update($updata);

        $updata['is_default'] = 1;
        $map['id'] = $post['id'];
        Db::table('fa_pifa_peisongyuan')->where($map)->update($updata);
         $this->success("成功");
    }
    public function paidan()
    {
        $post = $this->request->request();


        $user = Db::table('fa_user')->find($this->auth->id);

        if ($user['group_id'] != 5) {
            // $this->error("你没有权限操作");
        }

        $map['orderstate'] = 1;
        $map['id'] = $post['id'];
        $map['peisong_id'] = 0;
        $updata['peisong_id'] = $post['peisong_id'];
        $updata['paidan_emo'] = $post['emo'];
        $updata['orderstate'] = 2;
        $res = Db::table('fa_pifa_order')->where($map)->update($updata);
        if ($res) {
            $this->success("派单成功");
        }else{
            $this->error("派单失败");
        }
    }
    public function moren()
    {
        $post = $this->request->request();
        $map['is_default'] = 1;
        $res = Db::table('fa_pifa_peisongyuan')->where($map)->find();
        $this->success("成功",$res);
    }

    public function deletepeisong()
    {
       $post = $this->request->request();

       $res = Db::table('fa_pifa_peisongyuan')->delete($post['id']);

       $this->success("成功");
    }

     public function peisongshow()
    {
        $post = $this->request->request();  

        $params['id'] = $post['id'];

        $info = Peisong::info($params);

        $info['diqu'] = $info['province_name'].'-'.$info['city_name'].'-'.$info['area_name'];

        $this->success('地址详情', $info);
    }


     //添加修改收货地址
    public function editpeisong(){
        $post = $this->request->request();
        


        $rule = [
                'consignee'  => 'require',
                'phone'  => 'require|regex:/^1\d{10}$/',
                'province_id'     => 'require',
                'diqu'     => 'require',
        ];
        $msg = [         
                'consignee.require' => '请输入姓名',
                'phone.require' => '请填写手机号',
                'phone.regex' => '请输入正确的手机号',
                'diqu.require' => '请选择地区',
              
        ];
        $data = $post;
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            $this->error(__($validate->getError()));
        }



        $map['mobile'] = $post['phone'];

        $user = Db::table('fa_user')->where($map)->find();

        if (empty($user)) {
            $this->error('未找到该手机号的会员');
        }

        if ($user['group_id'] != 4) {
           $this->error('该会员不是配送员');
        }

      
        if (empty($post['id'])) {
            $params['user_id'] = $user['id'];
            $params['consignee'] = $post['consignee'];
            $params['phone'] = $post['phone'];
            $params['area_text'] = $post['diqu'];
            $params['address'] = $post['address'];
            $params['idcard'] = $post['idcard'];
            $params['jianjie'] = $post['jianjie'];
            $params['latitude'] = "";
            $params['id'] = 0;

            $params['is_default'] = '1';
        
            $this->success("添加成功",Peisong::edit($params));
        }else{
            $params['user_id'] = $user['id'];
            $params['consignee'] = $post['consignee'];
            $params['phone'] = $post['phone'];
            $params['area_text'] = $post['diqu'];
            $params['address'] = $post['address'];
            $params['idcard'] = $post['idcard'];
            $params['jianjie'] = $post['jianjie'];
            $params['latitude'] = "";
            $params['id'] = $post['id'];
            $params['is_default'] = '1';

            $this->success('编辑成功', Peisong::edit($params));
        }


    }


    
}
